#ifndef SESSION_H_
#define SESSION_H_

#include <stdint.h>
#include <uv.h>

#include "define.h"
#include "spidermessage.h"
enum
{
    SESSION_STATUS_INIT     = 0x00,
    SESSION_STATUS_CONN     = 0x01,
    SESSION_STATUS_TARGET   = 0x02,
    SESSION_STATUS_RELAY    = 0x03,
    SESSION_STATUS_FIN      = 0x04,
};

enum
{
    SESSION_MODEL_TCP = 0x01,
    SESSION_MODEL_UDP = 0x02,
};

class Spider;
class Session
{
private:       
    uv_timer_t m_sessionTimer;
    int m_commandTimeout;
    int m_idleTimeout;
public:
    Spider *m_spider;
    uint32_t m_sessionId;
    uint8_t m_status;
    uint64_t m_lastRecvTick;
    uint8_t m_model;
    char m_targetInfo[TARGET_INFO_SIZE];
    uint16_t m_targetPort;
       
    uv_getaddrinfo_t m_resolver;
    uv_connect_t m_connectTcpReq;
    uv_tcp_t m_connectTcp;
    uv_connect_t m_targetTcpReq;
    uv_tcp_t m_targetTcp;

    uv_udp_t m_targetUdp;
    bool m_hastarget;
    struct sockaddr m_targetAddr;

    uv_buf_t m_connectBuf;
    uv_buf_t m_targetBuf;

    uint32_t m_connectRecvLen;
    uint32_t m_targetRecvLen;
    uint16_t m_udpHeaderLen;    
public:
    Session()
    {
        m_status = SESSION_STATUS_INIT;
        m_lastRecvTick = 0;
        m_connectRecvLen = 0;
        m_hastarget = false;
        m_targetRecvLen = 0;
        m_udpHeaderLen = 0;
        m_model = 0;
    };
    ~Session() {};
    bool Init(Spider *spider, uint8_t model, uint8_t *data);
    void OnSessionCheck();

    void Close();

    void DoMessageTcp(MessageTcp *msg);
    void DoTcpConnectTarget(struct sockaddr *target);
    void DoTcpConnectedBoth();

    void DoMessageUdp(MessageUdp *msg);
    void DoUdpConnectedBoth();

    void DoUdpSendToTarget(struct sockaddr *target);

    void SendSignalToDataConn();
};

#endif